import configparser
import os
import yaml


class Data:
    # 获取文件绝对路径
    def get_absolute_path(cls, file_path):
        root_path = os.path.realpath(
            os.path.dirname(
                os.path.dirname(__file__)))
        path = os.path.join(root_path, file_path)
        return path

    # 读取yaml文件
    @classmethod
    def load_yaml(cls, file_path):
        path = cls.get_absolute_path(cls, file_path)
        with open(path) as f:
            data = yaml.safe_load(f)
        return data

    # 读取ini文件
    @classmethod
    def load_init(cls, file_path):
        path = cls.get_absolute_path(cls, file_path)
        # 实例化一个对象
        config = configparser.ConfigParser()
        # 读取文件，设置编码
        config.read(path, encoding = "utf-8")
        return config
